const { sequelize, QueryTypes } = require('../libs/mysql');


const addLogUser = async (params) => {
    let query = `insert into log_user (account_id,number,mydk_combofield,person,realname,mobile,usertype,systemtype,user_id,dept_number,dept_name,all_verification_auth,outbound_auth,outbound_type,status) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`;
    let paramsArray=[],i=0;
    paramsArray[i++]=params.accountId;
    paramsArray[i++]=params.number || null;
    paramsArray[i++]=params.mydk_combofield || null;
    paramsArray[i++]=params.person || null;
    paramsArray[i++]=params.name || null;
    paramsArray[i++]=params.mobile;
    paramsArray[i++]=params.usertype || null;
    paramsArray[i++]=params.systemtype || null;
    paramsArray[i++]=params.id;
    paramsArray[i++]=params.deptNumber || null;
    paramsArray[i++]=params.deptName || null;
    paramsArray[i++]=params.all_verification_auth || null;
    paramsArray[i++]=params.outbound_auth || null;
    paramsArray[i++]=params.outbound_type || null;
    paramsArray[i++]=params.status;
    const [results, metadata] =  await sequelize.query(query,  {
        replacements: paramsArray,
        type: QueryTypes.INSERT
    });
    return metadata;
};

const getUserAccount = async (params) => {
    let query = ' select lu.mobile, p.* from log_user lu left join push_app_authorize p on lu.account_id = p.account_id ' +
        ' where lu.id is not null and lu.status = 1 and lu.mobile = ? ';
    let paramsArray=[],i=0;
    paramsArray[i++]=params.mobile;
    return await sequelize.query(query, { replacements: paramsArray, type: QueryTypes.SELECT });
};

const getUserAccountByAccountId = async (params) => {
    let query = ' select lu.mobile, p.* from log_user lu left join push_app_authorize p on lu.account_id = p.account_id ' +
        ' where lu.account_id is not null and lu.status = 1 and lu.account_id = ? limit 1';
    let paramsArray=[],i=0;
    paramsArray[i++]=params;
    return await sequelize.query(query, { replacements: paramsArray, type: QueryTypes.SELECT });
};

const getLogUser = async (params) => {
    let query = ' select * from log_user where id is not null and account_id = ? and user_id = ? ';
    let paramsArray=[],i=0;
    paramsArray[i++]=params.accountId;
    paramsArray[i++]=params.id;
    return await sequelize.query(query, { replacements: paramsArray, type: QueryTypes.SELECT });
};

const updateLogUser = async (params) => {
    let query = 'update log_user set number = ?, mydk_combofield = ?, person = ?, realname = ?, mobile = ?, ' +
        ' usertype = ?, systemtype = ?, dept_number = ?, dept_name = ?, all_verification_auth = ?, outbound_auth = ?, outbound_type = ? where user_id = ?';
    let paramsArray=[],i=0;
    paramsArray[i++]=params.number || null;
    paramsArray[i++]=params.mydk_combofield || null;
    paramsArray[i++]=params.person || null;
    paramsArray[i++]=params.name || null;
    paramsArray[i++]=params.mobile;
    paramsArray[i++]=params.usertype || null;
    paramsArray[i++]=params.systemtype || null;
    paramsArray[i++]=params.deptNumber || null;
    paramsArray[i++]=params.deptName || null;
    paramsArray[i++]=params.all_verification_auth || null;
    paramsArray[i++]=params.outbound_auth || null;
    paramsArray[i++]=params.outbound_type || null;
    paramsArray[i++]=params.id;
    const [results, metadata] =  await sequelize.query(query,  {
        replacements: paramsArray,
        type: QueryTypes.UPDATE
    });
    return metadata;
};

const updateLogUserStatus = async (params) => {
    let query = 'update log_user set status = ? where user_id = ?';
    let paramsArray = [], i = 0;
    paramsArray[i++] = params.status;
    paramsArray[i++] = params.id;
    const [results, metadata] = await sequelize.query(query, {
        replacements: paramsArray,
        type: QueryTypes.UPDATE
    });
    return metadata;
};

const getAccountUserCount = async (params) => {
    let accountUserCount = {
        // 购买用户数
        accountUserCount: 0,
        // 已使用用户数
        usedAccountUserCount: 0
    };
    // 已使用用户数=======================================================================================================
    let queryUsedAccountUserCount = 'SELECT COUNT(*) as count FROM log_user WHERE account_id = ?';
    let paramsUsedAccountUserCount = [], y = 0;
    paramsUsedAccountUserCount[y++] = params.accountId;
    const [resultsUsedAccountUserCount, metadataUsedAccountUserCount] = await sequelize.query(queryUsedAccountUserCount, {
        replacements: paramsUsedAccountUserCount,
        type: QueryTypes.SELECT
    });
    accountUserCount.usedAccountUserCount = resultsUsedAccountUserCount.count;
    // 已购买用户数=======================================================================================================
    let queryAccountUserCount = 'SELECT account_user_count as accountUserCount FROM push_app_authorize WHERE account_id = ?';
    let paramsAccountUserCount = [], i = 0;
    paramsAccountUserCount[i++] = params.accountId;
    const [resultsAccountUserCount, metadataAccountUserCount] = await sequelize.query(queryAccountUserCount, {
        replacements: paramsAccountUserCount,
        type: QueryTypes.SELECT
    });
    accountUserCount.accountUserCount = resultsAccountUserCount.accountUserCount;
    return accountUserCount;
};

module.exports = {
    addLogUser,
    getUserAccount,
    getLogUser,
    updateLogUser,
    updateLogUserStatus,
    getAccountUserCount,
    getUserAccountByAccountId
};
